<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span>文章分类</span>
        <el-button type="primary" size="mini" @click="addVisible = true"
          >添加分类</el-button
        >
      </div>
      <!-- 表格区域 -->
      <el-table :data="cateList" border stripe style="width: 100%">
        <el-table-column
          label="序号"
          type="index"
          width="100"
        ></el-table-column>
        <el-table-column label="分类名称" prop="cate_name"></el-table-column>
        <el-table-column label="分类别名" prop="cate_alias"></el-table-column>
        <el-table-column label="操作">
          <!-- 使用插槽，自定义该列的内容 -->
          <template v-slot="scope">
            <el-button
              type="primary"
              size="mini"
              @click="showEditDialog(scope.row)"
              >修改</el-button
            >
            <el-button
              type="danger"
              size="mini"
              @click="removeCate(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 添加分类的对话框 -->
    <el-dialog
      title="添加文章分类"
      :visible.sync="addVisible"
      width="40%"
      @close="addClose"
    >
      <!-- 添加的表单 -->
      <el-form
        :model="addForm"
        :rules="addRules"
        ref="addRef"
        label-width="80px"
      >
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="addForm.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="addForm.cate_alias"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" @click="addVisible = false">取 消</el-button>
        <el-button size="mini" type="primary" @click="addCate">添 加</el-button>
      </span>
    </el-dialog>
    <!-- 修改分类的对话框 -->
    <el-dialog title="修改文章分类" :visible.sync="editVisible" width="40%">
      <!-- 修改的表单 -->
      <!-- 注意：添加和修改的验证规则是一样的，所以使用了一套 -->
      <el-form
        :model="editForm"
        :rules="addRules"
        ref="editRef"
        label-width="80px"
      >
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="editForm.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="editForm.cate_alias"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" @click="editVisible = false">取 消</el-button>
        <el-button size="mini" type="primary" @click="editCate"
          >修 改</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "ArtCate",
  data() {
    return {
      cateList: [],
      // 控制添加对话框的显示与隐藏
      addVisible: false,
      // 控制修改对话框的显示与隐藏
      editVisible: false,
      // 添加表单的数据对象
      addForm: { cate_name: "", cate_alias: "" },
      // 添加表单的验证规则对象
      addRules: {
        cate_name: [
          { required: true, message: "请输入分类名称", trigger: "blur" },
          {
            pattern: /^\S{1,10}$/,
            message: "分类名必须是1-10位的非空字符",
            trigger: "blur"
          }
        ],
        cate_alias: [
          { required: true, message: "请输入分类别名", trigger: "blur" },
          {
            pattern: /^[a-zA-Z0-9]{1,15}$/,
            message: "分类别名必须是1-15位的字母数字",
            trigger: "blur"
          }
        ]
      },
      // 修改表单的数据对象
      editForm: { cate_name: "", cate_alias: "" }
    }
  },
  created() {
    this.getCateList()
  },
  methods: {
    // 获取表单数据
    async getCateList() {
      const { data: res } = await this.$http.get("/my/cate/list")
      if (res.code === 0) {
        console.log(res)
        this.cateList = res.data
      } else {
        this.$message.error(res.message)
      }
    },
    // 关闭对话框时清空
    addClose() {
      this.$refs.addRef.resetFields()
    },
    // 添加书籍分类
    addCate() {
      this.$refs.addRef.validate(async (vaild) => {
        if (!vaild) return
        const { data: res } = await this.$http.post(
          "/my/cate/add",
          this.addForm
        )
        if (res.code === 0) {
          this.$message.success(res.message)
          this.addVisible = false
          this.getCateList()
        } else {
          this.$message.error(res.message)
        }
      })
    },
    // 修改分类对话框关闭
    editClosed() {
      this.$refs.editRef.resetFields()
    },
    // 修改书籍分类
    showEditDialog(row) {
      this.editVisible = true
      this.editForm = Object.assign({}, row)
    },
    // 修改数据
    editCate() {
      this.$refs.editRef.validate(async (vaild) => {
        if (!vaild) return
        const { data: res } = await this.$http.put(
          "/my/cate/info",
          this.editForm
        )
        if (res.code === 0) {
          this.$message.success(res.message)
          this.editVisible = false
          this.getCateList()
        } else {
          this.$message.error(res.message)
        }
      })
    },
    // 删除数据
    removeCate(id) {
      this.$confirm("此操作将永久删除该书籍分类, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(async () => {
          const { data: res } = await this.$http.delete("/my/cate/del", {
            params: {
              id
            }
          })
          if (res.code === 0) {
            this.$message.success(res.message)
            this.getCateList()
          } else {
            this.$message.error(res.message)
          }
        })
        .catch(() => {})
    }
  }
}
</script>

<style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
